﻿using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AgileLogistics.View.Baoxiu
{
    /// <summary>
    /// WaitPage.xaml 的交互逻辑
    /// </summary>
    public partial class WaitPage : Page
    {
        Model.DBContext dbContext = new Model.DBContext();

        ViewModel.RepairViewModel vm = new ViewModel.RepairViewModel();
        public WaitPage()
        {
            InitializeComponent(); 
            vm = DataContext as ViewModel.RepairViewModel;

            //初始化数据

            vm.RepairList = dbContext.DB.Queryable<Model.Repairs>().Where(r => r.State != "已完成").ToList();
            vm.DepList = dbContext.DB.Queryable<Model.Depment>().ToList();
        }

        private void btn_JieDan_Click(object sender, RoutedEventArgs e)
        {
            if(vm.CurrRepair==null)
            {
                Growl.WarningGlobal("请选择维修单！");
                return;
            }
            if (vm.CurrRepair.IsCheck == false)
            {
                Growl.WarningGlobal("该维修单还未审核！");
                return;
            }


            vm.CurrRepair.State = "已接单";
            dbContext.DB.Updateable<Model.Repairs>(vm.CurrRepair).ExecuteCommand();

            Growl.SuccessGlobal("状态变更成功！");

            vm.RepairList = dbContext.DB.Queryable<Model.Repairs>().Where(r => r.State != "已完成").ToList();
        }

        private void btn_Chuli_Click(object sender, RoutedEventArgs e)
        {
            if (vm.CurrRepair == null)
            {
                Growl.WarningGlobal("请选择维修单！");
                return;
            }
            if (vm.CurrRepair.IsCheck == false)
            {
                Growl.WarningGlobal("该维修单还未审核！");
                return;
            }


            vm.CurrRepair.State = "处理中";
            dbContext.DB.Updateable<Model.Repairs>(vm.CurrRepair).ExecuteCommand();

            Growl.SuccessGlobal("状态变更成功！");

            vm.RepairList = dbContext.DB.Queryable<Model.Repairs>().Where(r => r.State != "已完成").ToList();
        }

        private void btn_ChuliWancheng_Click(object sender, RoutedEventArgs e)
        {
            if (vm.CurrRepair == null)
            {
                Growl.WarningGlobal("请选择维修单！");
                return;
            }
            if (vm.CurrRepair.IsCheck == false)
            {
                Growl.WarningGlobal("该维修单还未审核！");
                return;
            }


            vm.CurrRepair.State = "已完成";
            dbContext.DB.Updateable<Model.Repairs>(vm.CurrRepair).ExecuteCommand();

            Growl.SuccessGlobal("状态变更成功！");

            vm.RepairList = dbContext.DB.Queryable<Model.Repairs>().Where(r => r.State != "已完成").ToList();
        }

        private void btn_Bohui_click(object sender, RoutedEventArgs e)
        {

        }

        private void btn_Check_Click(object sender, RoutedEventArgs e)
        {
            if (vm.CurrRepair == null)
            {
                Growl.WarningGlobal("请选择维修单！");
                return;
            }

            vm.CurrRepair.IsCheck = true;
             


            vm.CurrRepair.State = "已审核";
            dbContext.DB.Updateable<Model.Repairs>(vm.CurrRepair).ExecuteCommand();

            Growl.SuccessGlobal("已审核！");

            vm.RepairList = dbContext.DB.Queryable<Model.Repairs>().Where(r => r.State != "已完成").ToList();
        }
    }
}
